package com.sharemarking.system.controller.advice;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import com.sharemarking.system.controller.MenuController;
import com.sharemarking.system.dto.ExceptionType;
import com.sharemarking.system.exception.ApiException;
import com.sharemarking.system.exception.ImageNotFoundException;

@ControllerAdvice(basePackageClasses=MenuController.class)
public class ControllerExceptionAdvice {
	
	private Logger logger=LoggerFactory.getLogger(ControllerExceptionAdvice.class);
	
	@ExceptionHandler(value=Exception.class)
	public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        ModelAndView mav = new ModelAndView();
        e.printStackTrace();
        logger.error("未知异常",e);
        mav.addObject("exception", e);
        mav.addObject("url", req.getRequestURL());
        mav.setViewName("system/errorPage");
        return mav;
    }
	
	@ExceptionHandler(value=ApiException.class)
	@ResponseBody
	public ResponseEntity<ExceptionType> defaultErrorHandler(ApiException e) {
		return new ResponseEntity<ExceptionType>(e.getExceptionType(),HttpStatus.BAD_REQUEST);
    }
	
	@ExceptionHandler(value=ImageNotFoundException.class)
	@ResponseStatus(value=HttpStatus.NOT_FOUND)
	public void imageNotFountException(ImageNotFoundException infe){

	}
	
}
